package com.es.test.conf;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;


/**
 * @author django
 * @date 2024/1/30
 * @description TODO
 */
@Configuration
public class ElasticsearchConfig {

    @Bean
    public ElasticsearchClient getElasticsearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "Test1234"));
        RestClientBuilder builder = RestClient.builder(new HttpHost("es-cn-nwy3ljri6000dpbwi.elasticsearch.aliyuncs.com", 9200))
                .setHttpClientConfigCallback(httpAsyncClientBuilder ->
                        httpAsyncClientBuilder.setMaxConnTotal(2)
                                .setMaxConnPerRoute(2)
                                .setDefaultCredentialsProvider(credentialsProvider)
                                .setKeepAliveStrategy((response, context) -> Duration.ofSeconds(30).toMillis())
                )
                .setRequestConfigCallback(requestConfigBuilder ->
                        requestConfigBuilder.setSocketTimeout(50000).setConnectTimeout(5000)
                );


        RestClient restClient = builder.build();

        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        return new ElasticsearchClient(transport);
    }

    public static void main(String[] args) {
        System.out.println("companysizepreference_searchresult".length());
    }

    @Bean
    public ElasticsearchAsyncClient elasticsearchAsyncClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "Test1234"));
        RestClientBuilder builder = RestClient.builder(new HttpHost("es-cn-nwy3ljri6000dpbwi.elasticsearch.aliyuncs.com", 9200))
                .setHttpClientConfigCallback(httpAsyncClientBuilder ->
                        httpAsyncClientBuilder.setMaxConnTotal(10)
                                .setMaxConnPerRoute(10))
                .setRequestConfigCallback(requestConfigBuilder ->
                        requestConfigBuilder.setSocketTimeout(50000).setConnectTimeout(5000)
                )
                .setHttpClientConfigCallback(httpClientBuilder ->
                        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

        RestClient restClient = builder.build();


        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        return new ElasticsearchAsyncClient(transport);
    }


}
